package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.CollectMapper;

import com.cskaoyan.model.Collect;
import com.cskaoyan.model.CollectExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description：实现会员收藏具体操作
 * @author     ：huzhouming
 * @date       ：2022/5/6 11:48
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;

    @Transactional
    @Override
    /**
     * @author: huzhouming
     * @description: 按uerId,valueId查找会员收藏信息
     * @date: 2022/5/6
     * @param paramBO
     * @param userId
     * @param valueId
     * @return com.cskaoyan.model.vo.DataVO<com.cskaoyan.model.Collect>
     */
    public DataVO<Collect> query(BaseParamBO paramBO, Integer userId, Integer valueId) {
        Integer page = paramBO.getPage();//页码
        Integer limit = paramBO.getLimit();//数据量
        String sort = paramBO.getSort();//排序列
        String order = paramBO.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null ) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
            criteria.andDeletedEqualTo(false);
        collectExample.setOrderByClause(sort + " " + order);

        List<Collect> list = collectMapper.selectByExample(collectExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Collect> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return DataVO.data(total, pages, limit, page, list);
    }
}
